/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.willsemionhand.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.db.entity.BdOnhandQuantity;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.database.datasource.DbContextHolder;
import com.lightkits.wms.client.DocumentServiceClient;
import com.lightkits.wms.database.entity.willsemiinterface.BdOnhandMediate;
import com.lightkits.wms.domain.saveonhand.repo.BdOnhandQuanService;
import com.lightkits.wms.domain.willsemionhand.GetWmsOnhandService;
import com.lightkits.wms.domain.willsemionhand.repo.WmsOnhandMediateService;
import com.lightkits.wms.dto.document.GenerateDocNumRequestType;
import com.lightkits.wms.dto.document.GenerateDocNumResponseType;
import com.lightkits.wms.dto.willsemionhand.model.WmsOnhandDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 魏静
 * @create 2019-07-24 16:47
 * @desc 获取wms现有量接口
 **/
@Service
public class GetWmsOnhandServiceImpl implements GetWmsOnhandService {
    private final DocumentServiceClient client;
    private final BdOnhandQuanService quanService;
    private final WmsOnhandMediateService mediateService;

    @Autowired
    public GetWmsOnhandServiceImpl(DocumentServiceClient client,
                                   WmsOnhandMediateService mediateService,
                                   BdOnhandQuanService quanService) {
        this.client = client;
        this.mediateService = mediateService;
        this.quanService = quanService;
    }


    /**
     * 获取传输现有量
     *
     * @return WmsOnhandDto
     */
    @Override
    public List<WmsOnhandDto> getOnhand() {
        List<BdOnhandQuantity> onhandQuantities = new BdOnhandQuantity().
                selectList(new EntityWrapper<BdOnhandQuantity>()
                        .eq(BdOnhandQuantity.TRANS_FLAG, false));
        if (ObjectUtil.isNullOrEmpty(onhandQuantities)) {
            return new ArrayList<>();
        }
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        GenerateDocNumRequestType request = new GenerateDocNumRequestType();
        request.setCount(1);
        request.setSize(4);
        request.setType("BOQ");
        request.setField1(df.format(new Date()));
        GenerateDocNumResponseType response = client.generateDocNumber(request);
        String batchLotNumber = response.getDocNumList().get(0);
        List<BdOnhandMediate> mediates = new ArrayList<>();
        List<WmsOnhandDto> onhandDtos = new ArrayList<>();
        onhandQuantities.forEach(target -> {
            mediates.add(convertOnhandMediate(target, batchLotNumber));
            onhandDtos.add(convertOnhand(target));
            target.setTransFlag(true);

        });
        DbContextHolder.setDataSource("willsemi-interface");
        mediateService.insertBatch(mediates);
        DbContextHolder.clear();
        quanService.updateBatchById(onhandQuantities);
        return onhandDtos;
    }

    /**
     * 现有量的转换
     *
     * @param quantity 现有量
     * @return wms传出现有量
     */
    private WmsOnhandDto convertOnhand(BdOnhandQuantity quantity) {
        WmsOnhandDto onhandDto = new WmsOnhandDto();
        onhandDto.setFeatureType(quantity.getFeatureType());
        onhandDto.setOwnerType(quantity.getOwnerType());
        onhandDto.setPrimaryUom(quantity.getPrimaryUom());
        onhandDto.setStorageUnitCode(quantity.getStorageUnitCode());
        onhandDto.setUom(quantity.getUom());
        onhandDto.setWarehouseId(quantity.getWarehouseId().toString());
        onhandDto.setId(quantity.getId().toString());
        onhandDto.setItemId(quantity.getItemId().toString());
        onhandDto.setOrganizationId(quantity.getOrganizationId().toString());
        onhandDto.setMergeFlag(quantity.getMergeFlag());
        onhandDto.setPrimaryQuantity(quantity.getPrimaryQuantity());
        onhandDto.setQuantity(quantity.getQuantity());
        onhandDto.setFeatureValue(quantity.getFeatureValue());
        onhandDto.setLotNumber(quantity.getLotNumber());
        if (!ObjectUtil.isEmpty(quantity.getOwnerId())) {
            onhandDto.setOwnerId(quantity.getOwnerId().toString());
        }
        if (!ObjectUtil.isEmpty(quantity.getWmAreaId())) {
            onhandDto.setWmAreaId(quantity.getWmAreaId().toString());
        }
        return onhandDto;
    }

    /**
     * 现有量的转换
     *
     * @param quantity 现有量
     * @return wms传出现有量
     */
    private BdOnhandMediate convertOnhandMediate(BdOnhandQuantity quantity, String batchLotNum) {
        BdOnhandMediate onhandDto = new BdOnhandMediate();
        onhandDto.setFeatureType(quantity.getFeatureType());
        onhandDto.setFeatureValue(quantity.getFeatureValue());
        onhandDto.setLotNumber(quantity.getLotNumber());
        onhandDto.setOwnerType(quantity.getOwnerType());
        onhandDto.setPrimaryUom(quantity.getPrimaryUom());
        onhandDto.setStorageUnitCode(quantity.getStorageUnitCode());
        onhandDto.setUom(quantity.getUom());
        onhandDto.setWarehouseId(quantity.getWarehouseId());
        onhandDto.setOnHandId(quantity.getId());
        onhandDto.setItemId(quantity.getItemId());
        onhandDto.setOrganizationId(quantity.getOrganizationId());
        onhandDto.setMergeFlag(quantity.getMergeFlag());
        onhandDto.setPrimaryQuantity(quantity.getPrimaryQuantity());
        onhandDto.setQuantity(quantity.getQuantity());
        onhandDto.setOwnerId(quantity.getOwnerId());
        onhandDto.setWmAreaId(quantity.getWmAreaId());
        onhandDto.setBatchNumber(batchLotNum);
        return onhandDto;

    }

}
